package com.joysuch.wwyt.api.base.controller;

import com.joysuch.wwyt.api.base.vo.ApiConfigVo;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.entity.BaseConfig;
import com.joysuch.wwyt.core.enums.BaseConfigCodes;
import com.joysuch.wwyt.core.service.BaseConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jignwk
 * @since 2022/03/08 14:48
 */
@RestController
@RequestMapping("/api/base/config")
@Api(tags = "流程引擎的数据源接口")
public class ConfigController {

    @Autowired
    private BaseConfigService configService;

    @GetMapping("/findList")
    @ApiOperation(value = "查找基本配置Id")
    public ResultBean findByTypeId(Long tenantId, String orgCode) {
        BaseConfig buildIdConfig = configService.findConfig(tenantId, orgCode, BaseConfigCodes.LOCATE_CURRENT_BUILD_ID);
        BaseConfig mapTypeConfig = configService.findConfig(tenantId, orgCode, BaseConfigCodes.WWYT_SERVER_MAP_TYPE);
        List<ApiConfigVo> configVos = new ArrayList<>();
        configVos.add(ApiConfigVo.builder().code(buildIdConfig.getCode()).value(buildIdConfig.getValue()).build());
        configVos.add(ApiConfigVo.builder().code(mapTypeConfig.getCode()).value(mapTypeConfig.getValue()).build());
        return ResultBean.success(configVos);
    }


    @GetMapping("/findLocateList")
    @ApiOperation(value = "查找定位系统基本配置Id")
    public ResultBean findLocateList(Long tenantId, String orgCode) {
        BaseConfig urlConfigSave = configService.findFirstConfig(BaseConfigCodes.LOCATE_SERVER_WORK_AREA_SAVE);
        BaseConfig urlConfigDelete = configService.findFirstConfig(BaseConfigCodes.LOCATE_SERVER_WORK_AREA_DELETE);
        BaseConfig ipConfig = configService.findFirstConfig(BaseConfigCodes.LOCATE_SERVER_URL);
        BaseConfig protocolConfig = configService.findFirstConfig(BaseConfigCodes.LOCATE_API_SYNC_PROTOCOL);
        BaseConfig appIdConfig = configService.findFirstConfig(BaseConfigCodes.LOCATE_APP_ID);
        BaseConfig secretConfig = configService.findFirstConfig(BaseConfigCodes.LOCATE_SERVER_SECRET);
        BaseConfig buildIdConfig = configService.findFirstConfig(BaseConfigCodes.LOCATE_CURRENT_BUILD_ID);
        BaseConfig loginConfigV2 = configService.findFirstConfig(BaseConfigCodes.LOCATE_SERVER_V2_LOGIN_URL);
        BaseConfig loginConfig = configService.findFirstConfig(BaseConfigCodes.LOCATE_SERVER_LOGIN_URL);
        BaseConfig versionConfig = configService.findFirstConfig(BaseConfigCodes.LOCATE_VERSION);

        List<ApiConfigVo> configVos = new ArrayList<>();
        configVos.add(ApiConfigVo.builder().code(buildIdConfig.getCode()).value(buildIdConfig.getValue()).build());
        configVos.add(ApiConfigVo.builder().code(urlConfigSave.getCode()).value(urlConfigSave.getValue()).build());
        configVos.add(ApiConfigVo.builder().code(urlConfigDelete.getCode()).value(urlConfigDelete.getValue()).build());
        configVos.add(ApiConfigVo.builder().code(ipConfig.getCode()).value(ipConfig.getValue()).build());
        configVos.add(ApiConfigVo.builder().code(protocolConfig.getCode()).value(protocolConfig.getValue()).build());
        configVos.add(ApiConfigVo.builder().code(appIdConfig.getCode()).value(appIdConfig.getValue()).build());
        configVos.add(ApiConfigVo.builder().code(secretConfig.getCode()).value(secretConfig.getValue()).build());
        configVos.add(ApiConfigVo.builder().code(loginConfigV2.getCode()).value(loginConfigV2.getValue()).build());
        configVos.add(ApiConfigVo.builder().code(loginConfig.getCode()).value(loginConfig.getValue()).build());
        configVos.add(ApiConfigVo.builder().code(versionConfig.getCode()).value(versionConfig.getValue()).build());

        return ResultBean.success(configVos);
    }
}
